const cityData = require('../../../utils/picker.city.js');
import tui from '../../../common/httpRequest'

Page({
  data: {
    itemList: cityData,
    itemList2: [],
    receiveData: [],
    reset: 0,
    show: false,
    defaultItemList: [{
      "text": "北京市",
      "subText": "",
      "value": "110000",
      "src": "",
      "index": 0,
      "list": [{
        "text": "北京市",
        "value": "110000"
      }, {
        "text": "安徽省",
        "value": "340000"
      }]
    }, {
      "text": "北京市市辖区",
      "subText": "",
      "value": "110100",
      "src": "",
      "index": 0,
      "list": [{
        "text": "北京市市辖区",
        "value": "110100"
      }]
    }, {
      "text": "西城区",
      "subText": "",
      "value": "110102",
      "src": "",
      "index": 1,
      "list": [{
        "text": "东城区",
        "value": "110101"
      }, {
        "text": "西城区",
        "value": "110102"
      }, {
        "text": "朝阳区",
        "value": "110105"
      }]
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let itemList2 = [{
        src: '/static/images/basic/color.png',
        text: '高一(1)班',
        subText: '30人',
        value: 101
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(2)班',
        subText: '30人',
        value: 102
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(3)班',
        subText: '30人',
        value: 103
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(4)班',
        subText: '28人',
        value: 104
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(5)班',
        subText: '28人',
        value: 105
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(6)班',
        subText: '28人',
        value: 106
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(7)班',
        subText: '28人',
        value: 107
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(8)班',
        subText: '38人',
        value: 108
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(9)班',
        subText: '38人',
        value: 109
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(10)班',
        subText: '38人',
        value: 110
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(11)班',
        subText: '38人',
        value: 111
      },
      {
        src: '/static/images/basic/color.png',
        text: '高一(12)班',
        subText: '38人',
        value: 112
      }
    ];
    this.setData({
      itemList2: itemList2
    })
  },
  complete(e) {
    console.log(e);
    tui.toast('您选择的数据为：' + e.detail.text);
  },
  change(e) {
    console.log(e);
    /**
     *   layer: 0  第几级 index
       src: '/static/images/basic/color.png'
       subIndex: 2   //当前层级下选中项index
       subText: '30人'  //选中项数据
       text: '高一(3)班'
       value: 103 //选中项value数据
     * */

    // 模拟请求
    let value = e.detail.value;
    let layer = e.detail.layer;
    if (layer === 7) {
      //实际中以请求数据为准，无下级数据则传空数组
      this.setData({
        receiveData: []
      })
    } else {
      wx.showLoading({
        title: '请稍候...'
      });
      setTimeout(() => {
        wx.hideLoading();
        //请求完成后将数据处理成以下格式，传入，最后一级没有则传空数组
        switch (layer) {
          case 0:
            let receiveData = [{
                text: '张三',
                subText: '男',
                value: 11101
              },
              {
                text: '王五',
                subText: '男',
                value: 11102
              },
              {
                text: '周小小',
                subText: '女',
                value: 11103
              },
              {
                text: '周小小',
                subText: '女',
                value: 11103
              },
              {
                text: '周小小',
                subText: '女',
                value: 11103
              }
            ];
            this.setData({
              receiveData: receiveData
            })
            break;
          case 1:
            this.setData({
              receiveData: [{
                text: '他(她)说',
                value: 11101
              }]
            })
            break;
          case 2:
            this.setData({
              receiveData: [{
                text: '这是一个',
                value: 11101
              }]
            })
            break;
          case 3:
            this.setData({
              receiveData: [{
                text: '级联选择器',
                value: 11101
              }]
            })
            break;
          case 4:
            this.setData({
              receiveData: [{
                text: '测试例子',
                value: 11101
              }]
            })
            break;
          case 5:
            this.setData({
              receiveData: [{
                text: '总共',
                value: 11101
              }]
            })
            break;
          case 6:
            this.setData({
              receiveData: [{
                text: '8级数据',
                value: 11101
              }]
            })
            break;
          default:
            break;
        }
      }, 800);
    }
  },
  complete2(e) {
    console.log(e);
    tui.toast('您选择的数据为：' + e.detail.text);
  },
  selectAddr() {
    this.setData({
      show: true
    })
  },
  close() {
    this.setData({
      show: false
    })
  },
  complete3(e) {
    tui.toast('您选择的数据为：' + e.detail.text);
    this.close()
    setTimeout(() => {
      //重置数据
      this.setData({
        reset: this.data.reset + 1
      })
    }, 300)
  }
})